package com.jspgou.cms.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.springframework.stereotype.Repository;

import com.jspgou.cms.dao.StoreDao;
import com.jspgou.cms.entity.Store;
import com.jspgou.common.hibernate3.Finder;
import com.jspgou.common.hibernate3.HibernateBaseDao;
import com.jspgou.common.page.Pagination;

@Repository
public class StoreDaoImpl extends HibernateBaseDao<Store, Long> implements StoreDao {
	public Pagination getPage(int pageNo, int pageSize) {
		Criteria crit = createCriteria();
		Pagination page = findByCriteria(crit, pageNo, pageSize);
		return page;
	}
	
	public Pagination getPage(Long ctgId,int pageNo, int pageSize) {
		Finder f=Finder.create("select bean from Store bean ");
		if(ctgId!=null){
			f.append(" inner join bean.category node,Category parent");
			f.append(" where node.lft between parent.lft and parent.rgt");
			f.append(" and parent.id=:ctgId");
			f.setParam("ctgId", ctgId);
		}else{
			f.append(" where 1=1");
		}
		return find(f,pageNo,pageSize);
	}
	
	public Pagination getPage(Long ctgId,Boolean examine,int pageNo, int pageSize){
		Finder f=Finder.create("select bean from Store bean ");
		if(ctgId!=null){
			f.append(" inner join bean.category node,Category parent");
			f.append(" where node.lft between parent.lft and parent.rgt");
			f.append(" and parent.id=:ctgId");
			f.setParam("ctgId", ctgId);
		}else{
			f.append(" where 1=1");
		}
		if(examine!=null){
			f.append(" and bean.examine=:examine");
			f.setParam("examine", examine);
		}
		return find(f,pageNo,pageSize);
	}
	
	public Pagination getPage(Long ctgId,Boolean examine,Boolean forbidden,
			int pageNo, int pageSize){
		Finder f=Finder.create("select bean from Store bean ");
		if(ctgId!=null){
			f.append(" inner join bean.category node,Category parent");
			f.append(" where node.lft between parent.lft and parent.rgt");
			f.append(" and parent.id=:ctgId");
			f.setParam("ctgId", ctgId);
		}else{
			f.append(" where 1=1");
		}
		if(examine!=null){
			f.append(" and bean.examine=:examine");
			f.setParam("examine", examine);
		}
		
		if(forbidden!=null){
			f.append(" and bean.forbidden=:forbidden");
			f.setParam("forbidden", forbidden);
		}
		return find(f,pageNo,pageSize);
	}

	public Store findById(Long id) {
		Store entity = get(id);
		return entity;
	}
	
	@SuppressWarnings("unchecked")
	public Store findByStorePath(String storePath){
		Finder f=Finder.create("select bean from Store bean where 1=1");
		if(StringUtils.isNotBlank(storePath)){
			f.append(" and bean.storeExt.storePath=:storePath");
			f.setParam("storePath", storePath);
		}
		List<Store> list=find(f);
		if(list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	//通过域名查找店铺
	@SuppressWarnings("unchecked")
	public Store findByDomainName(String domainName){
		Finder f=Finder.create("select bean from Store bean where 1=1");
		if(StringUtils.isNotBlank(domainName)){
			f.append(" and bean.storeExt.domain=:domainName");
			f.setParam("domainName", domainName);
		}
		List<Store> list=find(f);
		if(list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}

	public Store save(Store bean) {
		getSession().save(bean);
		return bean;
	}

	public Store deleteById(Long id) {
		Store entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}
	
	@Override
	protected Class<Store> getEntityClass() {
		return Store.class;
	}
}